﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Web.Script.Serialization;

public partial class ShopList : ExtBaseRpt
{
    string mid;
    sendweixin weixin = new sendweixin();
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            txtbdate.Text = DateTime.Now.ToString();
            txtedate.Text = DateTime.Now.ToString();
            //BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string str = " and cast(convert(varchar(20),a.createdate,23)as datetime) between '" + txtbdate.Text + "' and '" + txtedate.Text + "'";
        string sql = @"select u.weixinname,u.imgurl,p.pname,a.* from Bas_WxOrders a
                        inner join Bas_WeixinUser u on u.openid = a.openid
                        inner join Bas_prize p on p.pid = a.pid where 1=1 " + str+" order by a.createdate desc";
        QueryData(this.Store1, mid, sql);
    }

    protected void BtnWeiXinUser(object sender, DirectEventArgs e)
    {
        try
        {
            //查询本地没有的新粉丝
            string sql = @"select *from Bas_WeixinUser where isnull(weixinname,'') = ''";
            DataSet ds = mydb.DM_GetDateSet(sql);
            if (ds != null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //通过openid获取用户资料明细
                    string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="
                                 + weixin.getwxtokan() + "&openid=" + dr["openid"].ToString().Trim() + "&lang=zh_CN";
                    string getmsg = weixin.GetModel(url);
                    if (getmsg != null && getmsg.Trim() != "")
                    {
                        try
                        {
                            //解析订单明细的josn
                            JavaScriptSerializer serializer = new JavaScriptSerializer();
                            JsonObject_info joif = serializer.Deserialize<JsonObject_info>(getmsg);
                            if (joif != null)
                            {
                                sql = " update Bas_WeixinUser set weixinname='" + joif.nickname
                                    + "' ,imgurl='" + joif.headimgurl + "' where openid='" + dr["openid"] + "'";
                                mydb.DM_ExecuteNonQuery(sql);
                            }
                        }
                        catch (Exception ex) { throw ex; }
                    }
                }
            }
            ShowBox("获取成功！");
            BtnQureyUser(null, null);
        }
        catch (Exception ex) { throw ex; }
    }
    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
}

public class JsonObject_info
{
    public string subscribe { get; set; }
    public string openid { get; set; }
    public string nickname { get; set; }
    public string sex { get; set; }
    public string language { get; set; }
    public string city { get; set; }
    public string province { get; set; }
    public string country { get; set; }
    public string headimgurl { get; set; }
    public string subscribe_time { get; set; }
    public string remark { get; set; }
}